home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / pc / windows / qtw_201 / setup / samples / mplayer / playmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-19  |  16.3 KB  |  504 lines

  1.  
  2. // ---------------------------------------------------------------------
  3. //
  4. // PlayMain.c - Movie Player - QuickTime for Windows
  5. //
  6. //              Version 1.0
  7. //
  8. //              (c) Copyright 1988-1994 Apple Computer, Inc. All Rights Reserved.
  9. //
  10. // ---------------------------------------------------------------------
  11.  
  12.  
  13. // Includes
  14. // --------
  15. #include <Windows.h> // Required by Windows
  16.  
  17. #include <qtw.h>   // Interface to QuickTime
  18. #include <qtole.h> // Interface to qtole dll's
  19.  
  20. #include "common.h" // Interface to common.c routines
  21.  
  22. #include "player.h"  // Interface to other *.c files
  23. #include "player.hr" // Defines used in *.rc files
  24.  
  25.  
  26. // Message-Persistent Data
  27. // -----------------------
  28. static struct // Hungarian notation: g
  29.   {HINSTANCE      hInstance;          // Instance handle
  30.    HINSTANCE      hResources;         // Resource-only DLL handle
  31.    HWND           hwndFrame;          // Frame window handle
  32.    HWND           hwndClient;         // MDI client window
  33.    HMENU          hMenu;              // Frame window menu
  34.    HACCEL         hAccel;             // Frame window accelerators
  35.    HWND           hActiveModelessDlg; // Handle of active modeless dlg if any
  36.    QTOLE_OLEDATA  qtoleOleData;       // OLE data struct
  37.  
  38.   } g;
  39.  
  40.  
  41. // Internal Function Declarations
  42. // ------------------------------
  43. static LPSTR NEAR PlayerParseCmdLine    (LPSTR);
  44. static  BOOL NEAR PlayerInitAppl        (VOID);
  45. static  HWND NEAR PlayerInitInst        (HINSTANCE, LPSTR, int);
  46. static  LONG NEAR PlayerTerminateInst   (VOID);
  47. static  BOOL NEAR DoQuickTimeInit       (HINSTANCE, LPSTR, LPINT);
  48. static  VOID NEAR KillQuickTime         (VOID);
  49.  
  50.  
  51. // Function: WinMain - Required Windows "Main" Routine
  52. // --------------------------------------------------------------------
  53. // Parameters: As required by Microsoft Windows
  54. //
  55. // Returns:    As required by Microsoft Windows
  56. // --------------------------------------------------------------------
  57. int PASCAL WinMain (HINSTANCE hInstance,
  58.                  HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  59.  
  60. // Define function data
  61.  
  62. {
  63.     MSG  msg; // Message
  64.  
  65.     g.hInstance = hInstance; // Initialize global data
  66.  
  67.     // Load resource-only DLL
  68.     if( !(g.hResources = CommonGetLocalizedResources
  69.         ( PLAYER_ROOT_NAME, hInstance, PLAYER_STRING_NOMEMORY ))) {
  70.         msg.wParam = 0;
  71.         goto bail;
  72.     }
  73.  
  74.     // Perform one-time initialization
  75.     if ( !hPrevInstance && !PlayerInitAppl() ) {
  76.         msg.wParam = 0;
  77.         goto bail;
  78.     }
  79.  
  80.     // Perform initializations that apply to a specific instance and create
  81.     // create window
  82.     if ( !PlayerInitInst( hPrevInstance, lpCmdLine, nCmdShow )) {
  83.         msg.wParam = 0;
  84.         goto bail;
  85.     }
  86.  
  87.     // Main message loop
  88.     while (GetMessage (&msg, NULL, NULL, NULL)) {
  89.         if (!(g.hActiveModelessDlg && IsWindow (g.hActiveModelessDlg) && IsDialogMessage( g.hActiveModelessDlg, &msg ))) {
  90.             if( !g.hwndClient ||
  91.                 !TranslateMDISysAccel( g.hwndClient, &msg )) {
  92.                 if( !g.hwndFrame ||
  93.                     !TranslateAccelerator( g.hwndFrame, g.hAccel, &msg )) {
  94.                     TranslateMessage (&msg);
  95.                     DispatchMessage  (&msg);
  96.                 }
  97.             }
  98.         }
  99.     }
  100.  
  101.     // Cleanup movie player
  102.  
  103.     PlayerTerminateInst();
  104.  
  105.  // Free the resource-only DLL
  106.  bail:
  107.     if ( g.hResources && ( g.hInstance != g.hResources ))
  108.         FreeLibrary( g.hResources );
  109.  
  110.     return msg.wParam;
  111. }
  112.  
  113.  
  114. // Function: PlayerInitAppl - Perform One-time Initialization
  115. // --------------------------------------------------------------------
  116. // Parameters: None
  117. //
  118. // Returns:    TRUE if OK, else FALSE
  119. // --------------------------------------------------------------------
  120. static BOOL NEAR PlayerInitAppl ( VOID )
  121.  
  122. {
  123.     WNDCLASS wc; // Window class information
  124.  
  125.     // Register the frame (main) window class
  126.  
  127.     wc.style         = 0;
  128.     wc.lpfnWndProc   = PlayerFrameWndProc;
  129.     wc.cbClsExtra    = 0;
  130.     wc.cbWndExtra    = 0;
  131.     wc.hInstance     = g.hInstance;
  132.     wc.hIcon         = LoadIcon( g.hResources,
  133.         MAKEINTRESOURCE( PLAYER_PLAYER_ICON ));
  134.     wc.hCursor       = LoadCursor( NULL, IDC_ARROW );
  135.     wc.hbrBackground = (HBRUSH) ( COLOR_APPWORKSPACE + 1 );
  136.     wc.lpszMenuName  = NULL;
  137.     wc.lpszClassName = PLAYER_FRAME_CLASS;
  138.  
  139.     if( !RegisterClass( &wc ))
  140.         return FALSE;
  141.  
  142.     // Register the movie window class
  143.  
  144.     wc.style         = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  145.     wc.lpfnWndProc   = PlayerMovieWndProc;
  146.     wc.cbClsExtra    = 0;
  147.     wc.cbWndExtra    = sizeof( VOID NEAR * );
  148.     wc.hInstance     = g.hInstance;
  149.     wc.hIcon         = NULL;
  150.     wc.hCursor       = NULL; // Set to NULL so we can set cursor in the wndproc
  151.     wc.hbrBackground = (HBRUSH) NULL;
  152.     wc.lpszMenuName  = NULL;
  153.     wc.lpszClassName = PLAYER_MOVIE_CLASS;
  154.  
  155.     return RegisterClass( &wc );
  156. }
  157.  
  158.  
  159. // Function: PlayerInitInst - Perform Instance Initialization
  160. // --------------------------------------------------------------------
  161. // Parameters: HINSTANCE hPrevInstance;  Previous instance
  162. //             LPSTR     lpCmdLine;      -->Command line arguments
  163. //             int       nCmdShow;       Parameter for first ShowWindow()
  164. //
  165. // Returns:    HWND      hwndFrame;      Frame window handle
  166. //                                       or NULL if initialization failed
  167. // --------------------------------------------------------------------
  168. static HWND NEAR PlayerInitInst
  169.               ( HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
  170.  
  171. {
  172.     // Do QuickTime Initializations.
  173.     if( !DoQuickTimeInit( hPrevInstance, lpCmdLine, &nCmdShow ) )
  174.         return NULL;
  175.  
  176.     // get menu and accelerators from localized resource
  177.     g.hAccel = LoadAccelerators ( g.hResources,
  178.         MAKEINTRESOURCE( PLAYER_ACCELERATORS ));
  179.     g.hMenu  = LoadMenu( g.hResources,
  180.         MAKEINTRESOURCE( PLAYER_FRAME_MENU ));
  181.     if( !g.hAccel || !g.hMenu ) {
  182.         CommonTellUser( g.hResources, PLAYER_STRING_NOACCELORMENU,
  183.             PLAYER_STRING_CAPTION, MB_OK );
  184.         if (g.hMenu) {
  185.             DestroyMenu(g.hMenu);
  186.             g.hMenu = NULL;
  187.         }
  188.         return NULL;
  189.     }
  190.  
  191.     // Create a main window for this application instance.
  192.     if ( !(g.hwndFrame = CreateWindow( PLAYER_FRAME_CLASS, "",
  193.         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  194.         CW_USEDEFAULT, CW_USEDEFAULT,
  195.         CW_USEDEFAULT, CW_USEDEFAULT,
  196.         NULL, g.hMenu, g.hInstance, NULL ))) {
  197.         CommonTellUser( g.hResources, PLAYER_STRING_NOWINDOW,
  198.             PLAYER_STRING_CAPTION, MB_OK );
  199.         if (g.hMenu) {
  200.             DestroyMenu(g.hMenu);
  201.             g.hMenu = NULL;
  202.         }
  203.         return NULL;
  204.     }
  205.  
  206.     // Tell qtole.dll the server hwnd
  207.     QTOLE_SetApplicationHwnd( &g.qtoleOleData, g.hwndFrame );
  208.  
  209.     // get MDI client window created during WM_CREATE message processing
  210.     g.hwndClient = PlayerQueryClientWindow(); // this is in FrameWnd.c
  211.  
  212.     // Display the frame window
  213.     // If ole started the app, the window will be initially hidden
  214.     ShowWindow  ( g.hwndFrame, nCmdShow );
  215.     if( nCmdShow != SW_HIDE )
  216.         UpdateWindow( g.hwndFrame );
  217.  
  218.     // Check command line for movie file.
  219.     // Note: this must come after ShowWindow, UpdateWindow
  220.     if( lpCmdLine = PlayerParseCmdLine( lpCmdLine ))
  221.         SendMessage( g.hwndFrame, WM_PLAYER_CMDLINE,
  222.         0, (LPARAM) lpCmdLine );
  223.  
  224.     return g.hwndFrame;
  225. }
  226.  
  227. // Function: PlayerParseCmdLine - Parse the command line. Return -> to
  228. //                                first argument and ignore any extras.
  229. //                                The default extension is appended if
  230. //                                name has no extension
  231. // --------------------------------------------------------------------
  232. // Parameters: LPSTR      lpCmdLine     command line pointer
  233. //
  234. // Returns:    LPSTR      lpCmdLine     command line pointer to first
  235. //                                      argument, else NULL
  236. // --------------------------------------------------------------------
  237. static LPSTR NEAR PlayerParseCmdLine( LPSTR lpCmdLine )
  238.  
  239. {
  240.     LPSTR  lpTemp;                        // Temp pointer
  241.     char   szExtension[FILE_EXT_LEN + 1]; // Default file extension
  242.     BOOL   bExtension;                    // Extension flag
  243.  
  244.     // Command line is already in Ansi char set even if entered from DOS
  245.     // command line
  246.  
  247.     // remove any leading blanks
  248.     while( *lpCmdLine == ' ' )
  249.         lpCmdLine = AnsiNext( lpCmdLine );
  250.  
  251.     if( *lpCmdLine == '\0' )
  252.         return NULL;
  253.  
  254.     // look for blank or end of string
  255.     bExtension = FALSE;
  256.     lpTemp = lpCmdLine;
  257.     while( *lpTemp && (*lpTemp != ' ' )) {
  258.         if( *lpTemp == '.' )
  259.             bExtension = TRUE;
  260.         lpTemp = AnsiNext( lpTemp );
  261.     }
  262.     *lpTemp = '\0';
  263.  
  264.     if( !bExtension ) {
  265.         LoadString( g.hResources,
  266.             PLAYER_STRING_FILEEXT, szExtension, sizeof( szExtension ));
  267.         lstrcat( lpCmdLine, szExtension );
  268.     }
  269.  
  270.     return lpCmdLine;
  271. }
  272.  
  273.  
  274. // Function: PlayerTerminateInst - Terminate Instance
  275. // --------------------------------------------------------------------
  276. // Parameters: VOID
  277. //
  278. // Returns:    Always 0L
  279. // --------------------------------------------------------------------
  280. static LONG NEAR PlayerTerminateInst( VOID )
  281.  
  282. {
  283.     // Clean up OLE
  284.     if( g.qtoleOleData.lpqtoleServer )
  285.         QTOLE_OLECleanUp( &g.qtoleOleData );
  286.  
  287.     // Cut the connections to QuickTime.
  288.     KillQuickTime();
  289.  
  290.     return 0L;
  291. }
  292.  
  293.  
  294. // The next two functions are used to initialize and kill QuickTime
  295.  
  296. // Function: DoQuickTimeInit - Establishes connections to QuickTime
  297. // --------------------------------------------------------------------
  298. // Parameters: HINSTANCE  hPrevInstance  Previous instance
  299. //             LPSTR      lpCmdLine      -> command line
  300. //             LPINT      lpnCmdShow     Parameter for first ShowWindow()
  301. //
  302. // Returns:    BOOL       TRUE if OK, else FALSE
  303. // --------------------------------------------------------------------
  304. static BOOL NEAR DoQuickTimeInit
  305.             ( HINSTANCE hPrevInstance, LPSTR lpCmdLine, LPINT lpnCmdShow )
  306.  
  307. {
  308.     OSErr       oserr;       // Temp return value
  309.     WORD        wIDString;   // ID of error message string
  310.     int         cQueue = 64; // Best queue size
  311.     QTOLE_INIT  qtoleInit;
  312.     QTOLE_ERR   qtole_err;
  313.     char        szCaption[30];
  314.  
  315.  
  316.     LoadString( g.hResources, PLAYER_STRING_CAPTION,
  317.         szCaption, sizeof( szCaption ));
  318.  
  319.     // Do OLE initialization before QTInitialize so we
  320.     // don't have to kill qt for install only
  321.     if( !( qtoleInit.fpServerCallBack = (QTOLEPROC) MakeProcInstance(
  322.         (FARPROC) QTOLEServerCallBack, g.hInstance ))) {
  323.         CommonTellUser( g.hResources, PLAYER_STRING_NOMEMORY,
  324.             PLAYER_STRING_CAPTION, MB_OK );
  325.         return FALSE;
  326.     }
  327.     else {
  328.         qtoleInit.lStructSize           = sizeof( qtoleInit );
  329.         qtoleInit.lVersion              = VERSION_1;
  330.         qtoleInit.hInstance             = g.hInstance;
  331.         qtoleInit.hResources            = g.hResources;
  332.         qtoleInit.lpCmdLine             = lpCmdLine;
  333.         qtoleInit.lpClassName           = PLAYER_FRAME_CLASS;
  334.         qtoleInit.lpServerCaption       = szCaption;
  335.         qtoleInit.lpnCmdShow            = lpnCmdShow;
  336.         qtoleInit.wIDFirstString        = OLE_STRING_FIRST;
  337.         qtoleInit.wIDFirstDlg           = OLE_DLG_FIRST;
  338.         qtoleInit.bMultipleObjectServer = TRUE;
  339.  
  340.         g.qtoleOleData.lStructSize      = sizeof( g.qtoleOleData );
  341.         g.qtoleOleData.lVersion         = VERSION_1;
  342.         g.qtoleOleData.wObjectType      = MOVIE_OBJECT;
  343.  
  344.         if( QTOLE_OK !=
  345.             ( qtole_err = QTOLE_Initialize( &g.qtoleOleData, &qtoleInit ))) {
  346.             if( QTOLE_INSTALL_ONLY != qtole_err )
  347.                 CommonTellUser( g.hResources,
  348.                 PLAYER_STRING_OLEINITFAILED,
  349.                 PLAYER_STRING_CAPTION, MB_OK );
  350.             return FALSE;
  351.         }
  352.     }
  353.  
  354.  
  355.     // Enlarge message queue to prevent occasional lose of posted messages
  356.     while( SetMessageQueue( cQueue-- ) == 0 );
  357.  
  358.     if( ( oserr = QTInitialize( NULL )) != QTI_OK ) {
  359.         switch( oserr ) {
  360.             case QTI_FAIL_NOEXIST:
  361.                 wIDString = PLAYER_STRING_QTWNOEXIST;
  362.                 break;
  363.  
  364.             case QTI_FAIL_CORRUPTDLL:
  365.                 wIDString = PLAYER_STRING_QTWBADDLL;
  366.                 break;
  367.  
  368.             case QTI_FAIL_286:
  369.                 wIDString = PLAYER_STRING_QTW286;
  370.                 break;
  371.  
  372.             case QTI_FAIL_WIN30:
  373.                 wIDString = PLAYER_STRING_QTWWIN30;
  374.                 break;
  375.  
  376.             default:
  377.                 wIDString = PLAYER_STRING_QTWFAILED;
  378.                 break;
  379.         }
  380.  
  381.         CommonTellUser( g.hResources, wIDString,
  382.             PLAYER_STRING_CAPTION, MB_OK );
  383.         return FALSE;
  384.     }
  385.  
  386.     if( EnterMovies() ) {
  387.         CommonTellUser( g.hResources, PLAYER_STRING_ENTMOVFAILED,PLAYER_STRING_CAPTION, MB_OK );
  388.         return FALSE;
  389.     }
  390.  
  391.     return TRUE;
  392. }
  393.  
  394. // Function: KillQuickTime - Cuts the connections to QuickTime
  395. // --------------------------------------------------------------------
  396. // Parameters: VOID
  397. //
  398. // Returns:    VOID
  399. // --------------------------------------------------------------------
  400. static VOID NEAR KillQuickTime( VOID )
  401.  
  402. {
  403.     ExitMovies();
  404.     QTTerminate();
  405.  
  406.     return;
  407. }
  408.  
  409.  
  410. //  This function is called by other modules whenever a modeless dialog
  411. //  is activated or deactivated
  412.  
  413. // Function: PlayerSetActiveModeless - Set handle of active modeless dlg
  414. // --------------------------------------------------------------------
  415. // Parameters: WPARAM   wParam          WA_ flag defined in Windows.h
  416. //             HWND     hModelessDlg    Handle of dlg being activated or
  417. //                                      deactivated
  418. //
  419. // Returns:    VOID
  420. // --------------------------------------------------------------------
  421. VOID FAR PlayerSetActiveModeless( WPARAM wParam, HWND hModelessDlg )
  422.  
  423. {
  424.     if( wParam != WA_INACTIVE )
  425.         g.hActiveModelessDlg = hModelessDlg;
  426.     else if( g.hActiveModelessDlg == hModelessDlg )
  427.         g.hActiveModelessDlg = NULL;
  428.  
  429.     return;
  430. }
  431.  
  432. // Function: PlayerNoMoreWindow - Sets global handles to NULL. Called during
  433. //                                frame window WM_DESTROY message processing
  434. // --------------------------------------------------------------------
  435. // Parameters: VOID
  436. //
  437. // Returns:    VOID
  438. // --------------------------------------------------------------------
  439. VOID FAR PlayerNoMoreWindow( VOID )
  440.  
  441. {
  442.     SetMenu(g.hwndFrame,NULL);
  443.     if (g.hMenu) {
  444.         DestroyMenu(g.hMenu);
  445.         g.hMenu = NULL;
  446.     }
  447.     g.hwndFrame  = NULL;
  448.     g.hwndClient = NULL;
  449.  
  450.     return;
  451. }
  452.  
  453. ///  The remaining functions are the query functions called by other modules
  454.  
  455. // Function: PlayerQueryInstance - Query Instance Handle
  456. // --------------------------------------------------------------------
  457. // Parameters: None.
  458. //
  459. // Returns:    HINSTANCE hInstance;    Application instance handle
  460. // --------------------------------------------------------------------
  461. HINSTANCE FAR PlayerQueryInstance( VOID )
  462.  
  463. {
  464.     return g.hInstance;
  465. }
  466.  
  467. // Function: PlayerQueryResources - Query Resource-Only DLL Handle
  468. // --------------------------------------------------------------------
  469. // Parameters: None.
  470. //
  471. // Returns:    HINSTANCE hResources    Resource-only DLL handle
  472. // --------------------------------------------------------------------
  473. HINSTANCE FAR PlayerQueryResources( VOID )
  474.  
  475. {
  476.     return g.hResources;
  477. }
  478.  
  479. // Function: PlayerQueryFrameWindow - Query Frame Window Handle
  480. // --------------------------------------------------------------------
  481. // Parameters: None.
  482. //
  483. // Returns:    HWND hwndFrame;          Frame window handle
  484. // --------------------------------------------------------------------
  485. HWND FAR PlayerQueryFrameWindow( VOID )
  486.  
  487. {
  488.     return g.hwndFrame;
  489. }
  490.  
  491. // Function: PlayerQueryOleData - Query -> to Ole Data struct
  492. // --------------------------------------------------------------------
  493. // Parameters: None.
  494. //
  495. // Returns:    LPQTOLE_OLEDATA        -> ole data struct
  496. // --------------------------------------------------------------------
  497. LPQTOLE_OLEDATA FAR PlayerQueryOleData( VOID )
  498.  
  499. {
  500.     return &g.qtoleOleData;
  501. }
  502.  
  503.  
  504.